<script type="text/javascript">
$(function(){
	var pg = App.soagov.svcInstIndex.ui, pp = pg.params
		, svcId = pp.id, gd = pg.find('.grid');

	gd.mac('grid', {
		cols : [{
			field: 'id', width: 32,title: '',
			render : function(r) {
				return '<input type="checkbox" name="keys" value="' + r.id + '"/>';
			}
		},{
			field: 'env', title: '所属环境', width: 80
		},{
			field: 'ip', title: '服务器IP', width: 120,
			render: function(r) {
				return $('<span class="a"></span>').click(function(){
					App.open({
						ctx: 'base',
						action: 'serverView',
						params: { id: r.serverId }
					});
				}).append(r.ip);
			}
		},{
			field: 'port', title: '应用端口', width: 80,
			render: function(r) {
				return $('<span class="a"></span>').click(function(){
					App.open({
						ctx: 'base',
						action: 'appView',
						params: { id: r.appId }
					});
				}).append(r.port);
			}
		},{
			field: 'limit', title: '主限流', width: 140,
			render: function(r) {
				var d = r.spyData;
				if (!d) {
					return '未知';
				}
				var c = d.svcConfig;
				if (c.slaConfigOn) {
					var s = d.tpsBucket.limit, 
						limit = '<span>' + (s > 99999? '无上限': s) + '</span>';
					var mon = $('<span class="a"></span>').click(function(){
						var p = pg.parent();
						r.svc = pp; //
						App.show({
							ctx: 'soagov',
							action: 'svcInstMonitor',
							params: r
						}, function(){
							App.show({
								ctx: 'soagov',
								action: 'svcInstIndex',
								params: pp
							}, $.noop, p);
						}, pg.parent());
					}).append('流量监控');
					return $('<div></div>').append(limit).append('&nbsp;').append(mon);
				}
				return '未开启';
			}
		},{
			field: 'status', title: '状态', width: 48,
			render: function(r) {
				return Cache.soagov.svc.inst.status[r.status];
			}
		},{
			field: 'opt', title: '操作', width: 260,
			render: function(r) {
				var del = $('<span class="a"></span>').click(function(){
					mac.confirm('确定要删除此实例吗？', function(){
						$.post(App.soagov.ctx + '/svc/inst/delete.do', {
							id: r.id
						}, function(rs){
							var ro = mac.eval(rs);
							if (ro.success) {
								gd.load();
							}
						});
					});
				}).append('删除');
				if (r.status == 0) { // down
					return del;
				}
				var up = $('<span class="a"></span>').click(function(){
					mac.confirm('确定要升级此实例吗？', function(){
						$.post(App.soagov.ctx + '/svc/inst/toggle.do', {
							act: 1, // ACT_UP
							id: r.id
						}, function(rs){
							var ro = mac.eval(rs);
							if (ro.success) {
								gd.load();
							} else {
								mac.alert('升级失败');
							}
						});
					});
				}).append('升级');
				var mock = $('<span class="a"></span>').click(function(){
					App.open({
						title: '服务结果模拟',
						ctx: 'soagov',
						action: 'svcInstMock',
						params: { id: r.id, svcId: svcId }
					}, gd.load);
				}).append('模拟');
				var down = $('<span class="a"></span>').click(function(){
					mac.confirm('确定要降级此实例吗？', function(){
						$.post(App.soagov.ctx + '/svc/inst/toggle.do', {
							act: 2, // ACT_DOWN
							id: r.id
						}, function(rs){
							var ro = mac.eval(rs);
							if (ro.success) {
								gd.load();
							} else {
								mac.alert('降级失败');
							}
						});
					});
				}).append('降级');
				var limit = $('<span class="a"></span>').click(function(){
					App.open({
						ctx: 'soagov',
						action: 'svcInstLimit',
						params: { id: r.id, svcId: svcId, data: r.spyData }
						// onClose: gd.load
					}, gd.load);
				}).append('限流');
				var bw = $('<span class="a"></span>').click(function(){
					App.open({
						ctx: 'soagov',
						action: 'svcInstBw',
						params: { id: r.id, svcId: svcId, data: r.spyData }
						// onClose: gd.load
					}, gd.load);
				}).append('黑白名单');
				var p = $('<div></div>').append(mock).append('&nbsp;')
					.append(del).append('&nbsp;').append(limit).append('&nbsp;')
					.append(bw).append('&nbsp;');
				switch (r.status) {
				case 1: // on
					return p.append(down);
				case 2: // off
					return p.append(up);
				case 3: // mocking
					return p.append(up).append('&nbsp;').append(down);
				}
			}
		}],
		loader: {
			url: App.soagov.ctx + '/svc/inst/list.do' ,
			params: {
				pageNo: 1, pageSize: 20, svcId: svcId, r: new Date().getTime()
			},
			autoLoad: true
		},
		pagerLength: 20
	});
	
	pg.seek('refresh').click(function(){
		gd.load();
	});
	pg.seek('monitor').click(function(){
		var p = pg.parent();
		App.show({
			ctx: 'soagov',
			action: 'svcInstMonitor',
			params: {
				addr: '', code: pp.url, svc: pp,
				spyData: { tpsBucketMap: [] }
			}
		}, function(){
			App.show({
				ctx: 'soagov',
				action: 'svcInstIndex',
				params: pp
			}, $.noop, p);
		}, pg.parent());
	});

	if (App.logon.power('svc.toggle')){
		var mm = $('<div></div>').mac('menu', {
			children: [{
				title: '升级',
				action: 1,
				icon: 'icon-disk'
			},{
				title: '降级',
				action: 2,
				icon: 'icon-disk'
			},{
				title: '模拟',
				action: 3,
				icon: 'icon-disk'
			},{
				title: '限流',
				action: 4,
				icon: 'icon-disk'
			},{
				title: '黑白名单',
				action: 5,
				icon: 'icon-disk'
			}]
		}).appendTo(pg);
		
		pg.seek('all').contextMenu({
			menu: mm,
			button: 1,
			anchor: 'el',
			offset: { x: 4, y: 30 }
		}, function(act, el, pos) {
			switch (act * 1) {
			case 3: //ACT_MOCK
				App.open({
					ctx: 'soagov',
					action: 'svcInstMock',
					params: { svcId: svcId }
				}, gd.load);
				return false;
			case 4: //ACT_LIMIT_SAVE
			case 5: //ACT_LIMIT_REMOVE
			case 6: //ACT_LIMIT_ON
			case 7: //ACT_LIMIT_OFF
				App.open({
					ctx: 'soagov',
					action: 'svcInstLimit',
					params: { svcId: svcId }
// 					onClose: gd.load
				}, gd.load);
				return false;
			case 6: //ACT_BW_ON
			case 7: //ACT_BW_OFF
				App.open({
					ctx: 'soagov',
					action: 'svcInstBw',
					params: { svcId: svcId }
				}, gd.load);
				return false;
			}
			mac.confirm('确定要' + el.text() + '吗？', function(){
				$.post(App.soagov.ctx + '/svc/inst/toggle.do', {
					act: act * 1,
					svcId: svcId
				}, function(rs){
					var ro = mac.eval(rs);
					if (ro.success) {
						gd.load();
					} else {
						mac.alert('“全部' + el.html()  + '”失败');
					}
				});
			});
			return false;
		}).show();
		
		pg.seek('batch').contextMenu({
			menu: mm,
			button: 1,
			anchor: 'el',
			offset: { x: 4, y: 30 }
		}, function(act, el, pos) {
			var ids = gd.seek('keys').filter(':checked').map(function(){
				return $(this).val();
			}).get().join(',');
			if (!ids){
				mac.alert('请至少选择一个实例');
				return;
			}
			mac.confirm('确定要' + el.html() + '吗？', function(){
				$.post(App.soagov.ctx + '/svc/inst/toggle.do', {
					act: act * 1,
					svcId: svcId,
					id: ids
				}, function(rs){
					var ro = mac.eval(rs);
	 				if (ro.success) {
	 					gd.load();
	 				} else {
	 					mac.alert('“批量' + el.html()  + '”失败');
	 				}
				});
			});
		}).show();
	}
	
	function adjust(){
		var h = pg.parent().height();
		gd.parent().height(h - 64);
		gd.adjust();
	}
	$(window).resize(adjust);
	adjust(); 
});
</script>
<div class="head">
	<button name="refresh">刷新</button>
	<button name="all">操作全部</button>
	<button name="batch">操作选中</button>
	<button name="monitor">总体监控</button>
</div>
<div class="clear"></div>
<div class="body">
	<div class="grid"></div>
</div>
